package com.cpecc.controller.wuzi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.cpecc.model.wuzi.ShenQing;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cpecc.manager.system.DepManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.ZheJiuFSManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.CaiGouManager;
import com.cpecc.manager.wuzi.FenTanManager;
import com.cpecc.manager.wuzi.HeTongManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.XiuGaiJLManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.manager.wuzi.ZheJiuManager;
import com.cpecc.manager.wuzi.ZhengMingManager;
import com.cpecc.model.system.User;
import com.cpecc.model.system.ZheJiuFS;
import com.cpecc.model.wuzi.CaiGou;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.XiuGaiJL;

@Controller
@RequestMapping(value = "/wuzi")
public class WuZiController {

	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private XiuGaiJLManager xiuGaiJLManager;
	
	@Autowired
	private ZheJiuFSManager zheJiuFSManager;
	
	@Autowired
	private FenTanManager fenTanManager;
	
	@Autowired
	private CaiGouManager caiGouManager;
	
	@Autowired
	private HeTongManager heTongManager;
	
	@Autowired
	private ZhengMingManager zhengMingManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private ZheJiuManager zheJiuManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@RequestMapping(value = "/wuZiDetail")
	public ModelAndView wuZiDetail(String id){
		ModelAndView mav=new ModelAndView();
		WuZi wuZi=wuZiManager.get(id);
		mav.addObject("wuZi", wuZi);
		Set<ShenQing> shenQingSet = wuZi.getShenQing();
		if (!CollectionUtils.isEmpty(shenQingSet)) {
			List<ShenQing> list = new ArrayList<>(shenQingSet);
			mav.addObject("shengQing", list.get(0));
		}
		mav.addObject("fenTanBMList", fenTanManager.getAllFenTanList(wuZi));
		mav.addObject("caiGouList", caiGouManager.list(wuZi));
		mav.addObject("yiJiaoList", yiJiaoManager.getAllYiJiaoByWuZi(wuZi));
		mav.addObject("tuiHuanList", yiJiaoManager.getAllTuiHuanByWuZi(wuZi));
		mav.addObject("xiuGaiJL", xiuGaiJLManager.getAllByWuZi(wuZi));
		mav.addObject("baoFeiJL", baoFeiManager.getAllByWuZi(wuZi));
		mav.addObject("diuShiJL", buPeiManager.getBuPeiByWuZi(wuZi));
		mav.addObject("weiXiuJL", weiXiuManager.getAllWeiXiuByWuZi(wuZi));
		mav.addObject("zheJiuJL", zheJiuManager.getAllZheJiuByWuZi(wuZi));
		mav.setViewName("wuzi/wuZiDetail");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouDetail")
	public ModelAndView caiGouDetail(String id){
		ModelAndView mav=new ModelAndView();
		CaiGou caiGou=caiGouManager.get(id);
		mav.addObject("caiGou", caiGou);
		mav.addObject("heTongList", heTongManager.list(caiGou));
		mav.addObject("fuJianList", zhengMingManager.list(caiGou));
		mav.setViewName("wuzi/caiGouDetail");
		return mav;
	}
	
	@RequestMapping(value = "/wuZiXiuGai")
	public ModelAndView wuZiXiuGai(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZi", wuZiManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("wuzi/wuZiXiuGai");
		return mav;
	}
	
	@RequestMapping(value = "/wuZiXiuGaiSave")
	public void wuZiXiuGaiSave(WuZi temp,String guanLiBMId,String baoXiuJZRQStr,String zheJiuLX,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WuZi yuan=wuZiManager.get(temp.getId());
		if(!temp.getWuZiMC().equals(yuan.getWuZiMC())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资名称");
			xiuGaiJL.setYuanZhi(yuan.getWuZiMC());
			xiuGaiJL.setXinZhi(temp.getWuZiMC());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiMC(temp.getWuZiMC());
		}
		if(yuan.getGuanLiBM()==null&&!guanLiBMId.equals("")){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("管理部门");
			xiuGaiJL.setYuanZhi("");
			xiuGaiJL.setXinZhi(depManager.get(guanLiBMId).getDepName());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuanLiBM(depManager.get(guanLiBMId));
		}else if(!guanLiBMId.equals(yuan.getGuanLiBM().getId())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("管理部门");
			xiuGaiJL.setYuanZhi(yuan.getGuanLiBM().getDepName());
			xiuGaiJL.setXinZhi(depManager.get(guanLiBMId).getDepName());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuanLiBM(depManager.get(guanLiBMId));
		}
		if(!temp.getPinPai().equals(yuan.getPinPai())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资品牌");
			xiuGaiJL.setYuanZhi(yuan.getPinPai());
			xiuGaiJL.setXinZhi(temp.getPinPai());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setPinPai(temp.getPinPai());
		}
		if(!temp.getZiChanBM().equals(yuan.getZiChanBM())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("资产编码");
			xiuGaiJL.setYuanZhi(yuan.getZiChanBM());
			xiuGaiJL.setXinZhi(temp.getZiChanBM());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setZiChanBM(temp.getZiChanBM());
		}
		if(!temp.getGuiGeXH().equals(yuan.getGuiGeXH())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("规格型号");
			xiuGaiJL.setYuanZhi(yuan.getGuiGeXH());
			xiuGaiJL.setXinZhi(temp.getGuiGeXH());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGuiGeXH(temp.getGuiGeXH());
		}

		if(temp.getWuZiYZ()!=yuan.getWuZiYZ()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资原值");
			xiuGaiJL.setYuanZhi(Double.toString(yuan.getWuZiYZ()));
			xiuGaiJL.setXinZhi(Double.toString(temp.getWuZiYZ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiYZ(temp.getWuZiYZ());
		}
		if(temp.getWuZiJingZhi()!=yuan.getWuZiJingZhi()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("物资净值");
			xiuGaiJL.setYuanZhi(Double.toString(yuan.getWuZiJingZhi()));
			xiuGaiJL.setXinZhi(Double.toString(temp.getWuZiJingZhi()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setWuZiJingZhi(temp.getWuZiJingZhi());
		}
		if(temp.getJingCanZhiLv()!=yuan.getJingCanZhiLv()){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("净残值率");
			xiuGaiJL.setYuanZhi(Float.toString(yuan.getJingCanZhiLv()));
			xiuGaiJL.setXinZhi(Float.toString(temp.getJingCanZhiLv()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setJingCanZhiLv(temp.getJingCanZhiLv());
		}
		if(!temp.getShengChanCJ().equals(yuan.getShengChanCJ())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("生产厂家");
			xiuGaiJL.setYuanZhi(yuan.getShengChanCJ());
			xiuGaiJL.setXinZhi(temp.getShengChanCJ());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setShengChanCJ(temp.getShengChanCJ());
		}
		if(!temp.getGongYingShangMC().equals(yuan.getGongYingShangMC())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("供应商名称");
			xiuGaiJL.setYuanZhi(yuan.getGongYingShangMC());
			xiuGaiJL.setXinZhi(temp.getGongYingShangMC());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setGongYingShangMC(temp.getGongYingShangMC());
		}


		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		try{
		    temp.setBaoXiuJZRQ(sdf.parse(baoXiuJZRQStr));
		}catch(Exception e){
		}
		if(yuan.getBaoXiuJZRQ()==null && temp.getBaoXiuJZRQ()!=null){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("保修截止日期");
			xiuGaiJL.setXinZhi(sdf.format(temp.getBaoXiuJZRQ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setBaoXiuJZRQ(temp.getBaoXiuJZRQ());
		}else if(yuan.getBaoXiuJZRQ()==null && temp.getBaoXiuJZRQ()==null){
			
		}else if(!sdf.format(temp.getBaoXiuJZRQ()).equals(sdf.format(yuan.getBaoXiuJZRQ()))){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("保修截止日期");
			xiuGaiJL.setYuanZhi(sdf.format(yuan.getBaoXiuJZRQ()));
			xiuGaiJL.setXinZhi(sdf.format(temp.getBaoXiuJZRQ()));
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setBaoXiuJZRQ(temp.getBaoXiuJZRQ());
		}

		if(!temp.getCunFangD().equals(yuan.getCunFangD())){
			XiuGaiJL xiuGaiJL=new XiuGaiJL();
			xiuGaiJL.setUser(user);
			xiuGaiJL.setWuZi(temp);
			xiuGaiJL.setXiuGaiXM("存放地点");
			xiuGaiJL.setYuanZhi(yuan.getCunFangD());
			xiuGaiJL.setXinZhi(temp.getCunFangD());
			xiuGaiJL.setXiuGaiRQ(new Date());
			xiuGaiJLManager.save(xiuGaiJL);
			yuan.setCunFangD(temp.getCunFangD());
		}
		yuan.setBeiZhu(temp.getBeiZhu());
		wuZiManager.cover(yuan);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
